package com.lib.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.lib.model.dto.DataBoardDto;

public class DataBoardDao extends SqlMapClientDaoSupport implements
		IDataBoardDao {

	@Override
	public ArrayList<DataBoardDto> getAllDataBoardList(int startPage,
			int startBlock, String searchType, String searchValue) {
		ArrayList<DataBoardDto> list = null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startpage", startPage);
		map.put("endpage", startBlock);
		map.put("searchType", searchType);
		map.put("searchValue", searchValue);

		list = (ArrayList<DataBoardDto>) getSqlMapClientTemplate()
				.queryForList("DataBoard.getAllDataBoardList", map);

		return list;
	}

	@Override
	public int dataBoardListCount(String searchType, String searchValue) {
		int count = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchType", searchType);
		map.put("searchValue", searchValue);

		count = (Integer) getSqlMapClientTemplate().queryForObject(
				"DataBoard.dataBoardListCount", map);

		return count;
	}

	@Override
	public DataBoardDto getDataBoardDetail(String databoard_num) {
		DataBoardDto dDto = null;

		dDto = (DataBoardDto) getSqlMapClientTemplate().queryForObject(
				"DataBoard.getDataBoardDetail", databoard_num);

		return dDto;
	}

	@Override
	public boolean setDataBoard(DataBoardDto dto) {
		int count = 0;
		try {

			getSqlMapClientTemplate().insert("DataBoard.setDataBoard", dto);
			count = 1;
		} catch (Exception e) {
			count = -1;

		}
		return count > 0 ? true : false;
	}

	@Override
	public boolean setUpdateDataBoard(DataBoardDto dto) {

		return false;
	}

	@Override
	public boolean downCount(String databoard_num) {
		int count = 0;
		try {
			getSqlMapClientTemplate().update("DataBoard.downCount",
					databoard_num);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count > 0 ? true : false;
	}

	@Override
	public boolean dataBoardCount(String databoard_num) {
		int count = 0;
		try {
			getSqlMapClientTemplate().update("DataBoard.dataBoardCount",
					databoard_num);
			count = 1;
		} catch (Exception e) {
			count = -1;
		}
		return count > 0 ? true : false;
	}

	@Override
	public boolean deleteData(String databoard_num) {

		boolean flag=false;
		try {
			getSqlMapClientTemplate().delete("DataBoard.deleteData", databoard_num);
			flag = true;
		} catch (Exception e) {
		}
		return flag;
	}

	@Override
	public boolean updateData(DataBoardDto dto) {
		
		boolean flag=false;
		try {
			getSqlMapClientTemplate().delete("DataBoard.updateData", dto);
			flag = true;
		} catch (Exception e) {
		}
		
		return flag;
	}

	@Override
	public ArrayList<DataBoardDto> homeData() {
		
		return (ArrayList<DataBoardDto>)getSqlMapClientTemplate().queryForList("DataBoard.homeData");
	}

}
